<template>
    <div>
        <div class="e-form">
            <BillTop  @cancel="handleClose"></BillTop>
            <div class="tabs" style="margin-bottom: 120px">
                <div class="tabs-title">基本信息</div>
                <div style="width: 100%" class="form">
                    <el-form v-if="!isRedSingle" :model="basicForm" :label-width="formLabelWidth"
                     ref="ruleForm"
                             :rules="rules"
                             class="demo-ruleForm"
                    >
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="机构：">
                                    <span>{{ basicForm.orgName }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="业务类型">
                                    <span>
                                        {{
                                            businessTypeValue(basicForm.businessTypeKey)
                                        }}
                                    </span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="盘点日期：" prop="inventoryDate">
                                      <el-date-picker
                                            value-format="yyyy-MM-dd"
                                            :clearable="false"
                                            type="date"
                                            placeholder="选择日期"
                                            v-model="basicForm.inventoryDate"
                                        ></el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                         <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="分包商：" prop="subcontractorName">
                                            <el-input
                                                v-model="
                                                    basicForm.subcontractorName
                                                "
                                            >
                                                <i
                                                    slot="suffix"
                                                    class="
                                                        el-input__icon
                                                        el-icon-document-copy
                                                    "
                                                    @click="clickSingle"
                                                ></i>
                                            </el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="盘点人员：">
                                                <el-input
                                                v-model="
                                                    basicForm.inventoryCreator
                                                "
                                            >
                                                <i
                                                    slot="suffix"
                                                    class="
                                                        el-input__icon
                                                        el-icon-document-copy
                                                    "
                                                    @click="selectPerson"
                                                ></i>
                                            </el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="合同：">
                                            <el-input
                                                readonly
                                                v-model="basicForm.contractName"
                                            ></el-input>
                                            <el-button
                                                class="a"
                                                @click="linkContract"
                                                >查看</el-button
                                            >
                                        </el-form-item>
                                    </el-col>
                                </el-row>

                    </el-form>
                       <el-form
                              v-else
                                :model="basicForm"
                                :label-width="formLabelWidth"
                                 ref="ruleForm"
                            >
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="盘点编号：">
                                            <span>{{ basicForm.billNo }}</span>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="机构：">
                                            <span>{{ basicForm.orgName }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="业务类型">
                                            <span>
                                                {{
                                                    basicForm.businessTypeValue
                                                }}
                                            </span>

                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="盘点日期：">
                                            <span>
                                                {{
                                                     basicForm.inventoryDate
                                                }}
                                            </span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>

                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="分包商：">
                                            <span>
                                                {{
                                                    basicForm.subcontractorName
                                                }}
                                            </span>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="盘点人员：">
                                            <span>
                                                {{ basicForm.inventoryCreator }}
                                            </span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>

                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="合同名称：">
                                            <span>{{
                                                basicForm.contractName
                                            }}</span>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="仓库：">
                                            <span>{{
                                                basicForm.warehouseName
                                            }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>

                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="备注：">
                                              <el-input
                                                type="textarea"
                                                v-model="basicForm.remarks"
                                                autocomplete="off"
                                            ></el-input>

                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                </div>
            </div>
            <div class="buttons">

                       <span class="tip"><span style="color: red; margin-right: 0">*</span>为必填项</span>
                <el-button
                    type="primary"
                    size="small"
                    style="background: #2e61d7"
                    @click="addSave"
                    >保存</el-button
                >
                <el-button size="small" @click="handleClose">关闭</el-button>
            </div>
        </div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import { showLoading, hideLoading } from '@/utils/common'
import otherRequest from '@/api/turnover/other.js'
import { selectDataClient, openAppClient } from '@/utils/common'
import {  selectgetUserUnderOrgPage } from '@/api/turnover/ppw.js'

export default {
    data () {
        return {
            //基础信息
            basicForm: {
                billId: '',
                billNo: '',
                businessTypeKey: '',
                businessTypeValue: '',
                contractId: '',
                contractName: '',
                contractNo: '',
                inventoryCreator: '',
                inventoryCreatorId: '',
                inventoryDate: new Date,
                nullifyCreated: '',
                nullifyCreator: '',
                nullifyCreatorId: '',
                nullifyDescription: '',
                nullifyReason: '',
                orgId: '',
                orgName: '',
                parentId: '',
                remarks: '',
                state: 0,
                redState: -1,
                subcontractorId: '',
                subcontractorName: '',
                warehouseId: '',
                warehouseName: ''
            },
            formLabelWidth: '180px',
            rules: {
                businessTypeKey: [
                    {
                        required: true,
                        message: '请选择业务类型',
                        trigger: 'change'
                    }
                ],
                inventoryDate: [
                    {
                        required: true,
                        message: '请选择盘点化日期',
                        trigger: 'change'
                    }
                ],
                subcontractorName: [
                    {
                        required: true,
                        message: '请填写',
                        trigger: 'change'
                    }
                ],

            },
            test: [],
            typeArr: [
                {
                    id: '1',
                    uname: '分包合同'
                },
                {
                    id: '2',
                    uname: '加工合同'
                },
                {
                    id: '3',
                    uname: '销售合同'
                },
                {
                    id: '4',
                    uname: '事实合同'
                },
            ]
        }
    },
    created () {
        this.$route.query.isRedSingle === '1' ? this.isRedSingle = true : this.isRedSingle = false
        if (this.isRedSingle) {
            this.getRedList()
        }
        this.getUserInfo()
    },
    computed: {
        ...mapState({
            options: state => state.contract.ctClassify
        }),
        businessTypeValue () {
            return param=>{
                const obj = this.typeArr.find(x => x.id == param)
                console.log(obj, 'obj')
                if (obj !== undefined) {
                    return obj.uname
                }
            }
        }
    },
    methods: {
        //获取本地登录信息
        getUserInfo () {
            const userInfo = JSON.parse(window.localStorage.getItem('vuex'))
                .userInfo.orgInfo
            const userName = window.localStorage.getItem('userName')
            const userId = window.localStorage.getItem('userId')
            this.basicForm.inventoryCreator = userName
            this.basicForm.inventoryCreatorId = userId

            this.basicForm.orgId = userInfo.orgId
            this.basicForm.orgName = userInfo.orgName
        },
        // 如果是点击冲红单进入的新增页,则需要返显数据,只有备注能修改,其余均不能修改
        async getRedList () {
            showLoading()
            try {
                let data = await otherRequest.turnoverInventoryInitRed(this.$route.query.billid)
                this.basicForm = data
            } finally {
                hideLoading()
            }
        },

        // 查看合同
        linkContract () {
            if(!this.basicForm.contractId) {
                this.clientPop('err', '请选择和合同')
                return
            }
            const linkData = {
                1: '/contractDefinitionShow', //分包合同
                2: '/processingContractApplyShow', //加工合同
                3: '/ctMaterialSalesSigningShow' //销售合同
            }
            const data = linkData[this.basicForm.businessTypeKey]
            let linkDatas =  `${data}?billid=${this.basicForm.contractId}&isview=true`
            console.log(linkDatas)
            openAppClient(
                `${data}?billid=${this.basicForm.contractId}&isview=true`,
                '合同',
                function () {}
            )
        },
        // 选分包商
        clickSingle () {
            selectDataClient({
                SelectServiceToken: '/turnover/receive/inventorySelectContract', // 客户端对应方法名
                IsSingleMode: true, // 是否单选 true 单选 false 多选
                SelectedIDs: [], // 已选中id(注意：是上一次已选中的id，不是要传的参数，如果第一次选，则传[])
                Title: '选择合同' // 标题
            }).then(val => {

                const data = val[0]
                this.test = data
                this.basicForm.contractId = data.contractId
                this.basicForm.contractName = data.contractName
                this.basicForm.businessTypeKey = data.contractType
                this.basicForm.businessTypeValue = this.businessTypeValue(data.contractType)

                this.basicForm.subcontractorName = data.receiveCompanyName
                this.basicForm.subcontractorId = data.receiveCompanyId
            })
        },

        selectPerson () {
            selectgetUserUnderOrgPage(this.basicForm.orgId).then(res=>{
                this.basicForm.inventoryCreator = res.userName
                this.basicForm.inventoryCreatorId = res.userId
            })
        },
        //基本信息保存
        addSave () {
            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    this.$Ajax
                        .httpPost({
                            url: '/turnover/inventory/create',
                            params: this.basicForm
                        })
                        .then(res => {
                            this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                                this.$router.push({
                                    path: '/revolveInventoryEdit',
                                    query: {
                                        billid: res,
                                        name: 'detailed'
                                    }
                                })
                            },
                            null,
                            this.handleClose
                            )
                        })

                }
            })
        },
        //取消
        handleClose () {
            this.$router.push('/revolveInventoryList')
        }
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
.e-table {
    min-height: auto;
    background: #fff;
}
.separ {
    width: 30px;
    height: 40px;
    line-height: 18px;
    text-align: center;
}
.upload {
    margin: 20px auto;
    display: flex;
    justify-content: center;
    text-align: center;
}

.upload-demo {
    display: flex;
    justify-content: center;
    align-items: center;
}

::v-deep.el-input--suffix .el-input__inner {
    padding-right: 5px;
}
::v-deep.el-table .cell {
    text-align: center;
}
</style>
